﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_RoyalItemHistories : System.Web.UI.UserControl
    {
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IPositionBLL posBLL = new PositionBLL();
        private IRoyalItemHistoriesBLL royalHis = new RoyalItemHistoriesBLL();

        private RadComboBox cmbRoyalItem;
        private object _dataItem = null; 
        protected void Page_Load(object sender, EventArgs e)
        {
            cmbRoyalItem = (RadComboBox)cbRoyalItem.FindControl("cbRoyalItem");

            if (DataItem != null)
            {
                cbRoyalItem.ListRoyalItemComboBox();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    cmbRoyalItem.SelectedValue = DataBinder.Eval(DataItem, "RoyalItem.Sequence").ToString();
                }
                else
                {

                }
                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }
        public object DataItem
        {
            get {return this._dataItem;}
            set {this._dataItem = value;}
        }

        protected void FillDataToEmployees()
        {
            //IList<EmployeeInfo> empList = empBLL.ListEmployees();
            //DataTable dt = empList;

            //foreach (DataRow dr in dt.Rows)
            //{
            //    RadComboBoxItem item = new RadComboBoxItem();

            //    item.Text = dr["EMP_NAME"].ToString();
            //    item.Value = dr["EMP_SEQ"].ToString();

            //    item.Attributes.Add("EMP_NAME", dr["EMP_NAME"].ToString());
            //    item.Attributes.Add("BIRTH_DATE", dr["BIRTH_DATE"].ToString());
            //    item.Attributes.Add("POS_NAME", dr["POS_NAME"].ToString());
            //    item.Attributes.Add("CLASS_NAME", dr["CLASS_NAME"].ToString());
            //    item.Attributes.Add("JOB_SITE_NAME", dr["JOB_SITE_NAME"].ToString());

            //    cmbEmpSeq.Items.Add(item);

            //    item.DataBind();
            //}
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            //royalHis.Insert(cmbEmpSeq.SelectedValue
            //    , 1
            //    , cmbRoyalItem.SelectedValue
            //    , DateTime.Parse(txtReceivedDate.Text)
            //    , txtPosRef.Text.Trim()
            //    , txtPosName.Text.Trim()
            //    , txtClassName.Text.Trim()
            //    , double.Parse(txtSalary.Text)
            //    , txtJobSiteName.Text.Trim()
            //    , txtGazetteNo1.Text
            //    , txtGazetteNo2.Text
            //    , txtGazetteNo3.Text
            //    , txtGazetteNo4.Text
            //    , txtGazetteNo5.Text
            //    , DateTime.Parse(txtGazetteDate.Text)
            //    , DateTime.Parse(txtRoyalDate.Text)
            //    , 1);
            royalHis.Insert(Session["emp_seq"].ToString() // ไม่ถูกต้อง เพราะควรจะต้องเลือกว่าจะเป็นข้อมูลของใคร ไม่ใช่ emp_seq ซึ่งเป็นของคนล็อกอิน, เมนูนี้จะซ่อน
                            , 1
                            , cmbRoyalItem.SelectedValue
                            , txtReceiveDate2.GetDateValue() //DateTime.Parse(txtReceivedDate.Text)
                            , "" //txtPosRef.Text.Trim()
                            , "" //txtPosName.Text.Trim()
                            , "" //txtClassName.Text.Trim()
                            , 0 //double.Parse(txtSalary.Text)
                            , "" //txtJobSiteName.Text.Trim()
                            , txtGazetteNo1.Text
                            , txtGazetteNo2.Text
                            , txtGazetteNo3.Text
                            , txtGazetteNo4.Text
                            , txtGazetteNo5.Text
                            , txtGazetteDate2.GetDateValue() // DateTime.Parse(txtGazetteDate.Text)
                            , txtRoyalDate2.GetDateValue() // DateTime.Parse(txtRoyalDate.Text)
                            , 1);

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            royalHis.UpdateHistory(Int32.Parse(lblSeq.Text.Trim())
                , cmbRoyalItem.SelectedValue
                , txtReceiveDate2.GetDateValue()// DateTime.Parse(txtReceiveDate.TextWithLiterals.Trim())
                , txtGazetteNo1.Text
                , txtGazetteNo2.Text
                , txtGazetteNo3.Text
                , txtGazetteNo4.Text
                , txtGazetteNo5.Text
                , txtGazetteDate2.GetDateValue() //DateTime.Parse(txtGazetteDate.TextWithLiterals.Trim())
                , txtRoyalDate2.GetDateValue()//DateTime.Parse(txtRoyalDate.TextWithLiterals.Trim())
                , 1);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cmbEmpSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //empInfo = emp.GetEmployeeInfo(Int32.Parse(cmbEmpSeq.SelectedValue));
            //posInfo = pos.GetPositionInfo(empInfo.PositionSeq);

            //txtPosRef.Text = posInfo.RefNo;
            //txtPosName.Text = posInfo.Name;
            //txtClassName.Text = empInfo.ClassName;
            //txtSalary.Text = empInfo.Salary.ToString();
            //txtJobSiteName.Text = posInfo.JobSiteName;
        }
    }
}